from typing import Optional, Dict, Any, List

from pydantic import BaseModel, Field


class BaseKnowledgeRequest(BaseModel):
    """知识库操作基础请求模型（包含公共参数）"""
    db_name: str = Field(default="my_knowledge_db", description="目标数据库名称")
    collection_name: str = Field(default="my_collection", description="目标集合名称")
    vector_dimension: int = Field(default=768, description="向量维度（需与Ollama嵌入模型匹配）")


class SingleTextRequest(BaseKnowledgeRequest):
    """单条文本录入请求模型"""
    text: str = Field(..., description="待录入的文本内容，不能为空")
    metadata: Optional[Dict[str, Any]] = Field(default=None, description="文本元数据（如来源、日期等）")
    doc_id: Optional[str] = Field(default=None, description="自定义文档ID（可选，不填则自动生成）")


class BatchTextRequest(BaseKnowledgeRequest):
    """批量文本录入请求模型"""
    texts: List[str] = Field(..., description="待录入的文本列表，不能为空")
    metadatas: Optional[List[Dict[str, Any]]] = Field(default=None, description="元数据列表（需与文本列表长度一致）")
    ids: Optional[List[str]] = Field(default=None, description="自定义ID列表（需与文本列表长度一致）")


class FileTextRequest(BaseKnowledgeRequest):
    """文件导入文本请求模型"""
    file_path: str = Field(..., description="本地文本文件路径（仅支持.txt，每行一条文本）")
    metadata: Optional[Dict[str, Any]] = Field(default=None, description="统一的元数据（如文件来源）")
    encoding: str = Field(default="utf-8", description="文件编码格式")


# 标准化返回
class KnowledgeResponse(BaseModel):
    """知识库操作响应模型"""
    success: bool = Field(..., description="操作是否成功")
    message: str = Field(..., description="操作结果描述")
    data: Optional[Dict[str, Any]] = Field(default=None, description="附加数据（如录入ID、文档总数等）")